﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Drawing.Design;
using NetJsWire.Lite.Converters;
using NetJsWire.Lite.Editors;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Data
{
    [JsonObject]
    [Serializable]
    public class Field
    {
        private bool _allowBlank = true;
        private string _sortDir = "ASC";
        private string _type = "auto";

        [DefaultValue( true )]
        [Description( "Used for validating a record, defaults to true." )]
        [JsonProperty( "allowBlank", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public bool AllowBlank
        {
            get { return _allowBlank; }
            set { _allowBlank = value; }
        }

        [DefaultValue( ( string ) null )]
        [Description( "A function which converts the value provided by the Reader into an object that will be stored in the Record." )]
        [JsonProperty( "convert", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        public string Convert { get; set; }

        [DefaultValue( ( string ) null )]
        [Description( "(Optional) Used when converting received data into a Date when the type is specified as 'date'." )]
        [JsonProperty( "dateFormat", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public string DateFormat { get; set; }

        [DefaultValue( null )]
        [Description( "The default value used when a Record is being created by a Reader when the item referenced by the mapping does not exist in the data object (i.e. undefined)." )]
        [TypeConverter( typeof( StringConverter ) )]
        [JsonProperty( "defaultValue", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public object DefaultValue { get; set; }

        [DefaultValue( ( string ) null )]
        [Description( "Optional) A path expression for use by the Ext.data.DataReader implementation that is creating the Record to extract the Field value from the data object. If the path expression is the same as the field name, the mapping may be omitted." )]
        [JsonProperty( "mapping", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public string Mapping { get; set; }

        [DefaultValue( ( string ) null )]
        [Description( "The name by which the field is referenced within the Record." )]
        [JsonProperty( "name", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public string Name { get; set; }

        [DefaultValue( "ASC" )]
        [Description( "Initial direction to sort ('ASC' or 'DESC'). Defaults to 'ASC'." )]
        [JsonProperty( "sortDir", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public string SortDir
        {
            get { return _sortDir; }
            set { _sortDir = value; }
        }

        [DefaultValue( ( string ) null )]
        [Description( "A function which converts a Field's value to a comparable value in order to ensure correct sort ordering." )]
        [JsonProperty( "sortType", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public string SortType { get; set; }

        [DefaultValue( "auto" )]
        [Description( "The data type for automatic conversion from received data to the stored value if convert has not been specified." )]
        [JsonProperty( "type", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        [DefaultValue( false )]
        [Description( "(Optional) Use when converting received data into a Number type (either int or float)." )]
        [JsonProperty( "useNull", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public bool UseNull { get; set; }
    }
}
